﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Providers;
using BusinessLogicLayer.Base;
using Objects;
using DataDefinition;

namespace BusinessLogicLayer.Manager
{
    public class RatingManager : ManagerBase<RatingDataProvider>
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="RatingManager" /> class.
        /// </summary>
        public RatingManager() { } 
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new RatingDataProvider();
        }
        #endregion

        #region [Public Methods]
        /// <summary>
        /// Gets the article ratings by article id
        /// </summary>
        /// <param name="articleID">article id</param>
        /// <returns>article ratings list</returns>
        public List<GetArticleRatingByArticleID> GetArticleRatingByArticleID(int articleID)
        {
            //The article rating details to be returned
            List<GetArticleRatingByArticleID> articleRatingDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetRatingByArticleID(articleID);

                if (dataSet != null && dataSet.GetRatingArticleID != null && dataSet.GetRatingArticleID.Rows != null)
                {
                    //Set the returned filled list
                    articleRatingDetails = FillArticleRatingsObjectsByEntities(dataSet.GetRatingArticleID);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the article rating details list
            return articleRatingDetails;
        }

        /// <summary>
        /// Update or insert an rating.
        /// </summary>
        /// <param name="articleRating">article rating object</param>
        /// <returns>true or false</returns>
        public bool UpdateRating(ArticleRating articleRating)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (articleRating.ArticleID > 0 && articleRating.Rating > 0)
                {
                    //Insert an article
                    success = base.DataProvider.InsertRating(articleRating.ArticleID, articleRating.UserID, articleRating.Rating, articleRating.IsActive);

                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }
        #endregion

        #region [Private Methods]
        /// <summary>
        /// Fills the article rating article id object
        /// </summary>
        /// <param name="dataTable">the data table</param>
        /// <returns>The article rating list</returns>
        private List<GetArticleRatingByArticleID> FillArticleRatingsObjectsByEntities(AutoFixDataSet.GetRatingArticleIDDataTable dataTable)
        {
            //The article rating list that contains the objects
            //Loop through the data table and create new ArticleRatingObject 
            //finally select the ArticleRatingObject and convert to list
            List<GetArticleRatingByArticleID> articleRatingDetails = (from aro in
                                                                        (from ar in dataTable.AsEnumerable<AutoFixDataSet.GetRatingArticleIDRow>()
                                                                       select new
                                                                       {
                                                                           ArticleRatingObject = new GetArticleRatingByArticleID()
                                                                           {
                                                                               Rating = (Int32)ar.ArticleRating
                                                                           }
                                                                       })
                                                                    select aro.ArticleRatingObject).ToList();
            //Return the filled article rating details list
            return articleRatingDetails;
        }

        #endregion
    }
}
